package com.ohos.connect.devicecloud.iot.modular.pro.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.ohos.connect.devicecloud.common.annotation.CommonLog;
import com.ohos.connect.devicecloud.common.pojo.CommonResult;
import com.ohos.connect.devicecloud.iot.modular.pro.result.BizDpProductDetailResult;
import com.ohos.connect.devicecloud.iot.modular.pro.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author MZQ
 * @Description
 * @Date 2025/6/4 10:56
 **/
@Api(tags = "产品")
@ApiSupport(author = "OHOS_CONNECT_TEAM", order = 1)
@RestController
@RequestMapping(value = "/api/v1/device_cloud/product")
@Validated
@Slf4j
public class ProductController {

    @Resource
    private ProductService productService;


    /**
     * 产品详情
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation("根据podId查看产品详情")
    @CommonLog("根据podId获取查看产品详情")
    @GetMapping("/get")
    public CommonResult<BizDpProductDetailResult> get(@RequestParam String prodId) {
        BizDpProductDetailResult detail = productService.getByProdId(prodId);
        if (ObjectUtil.isEmpty(detail)) {
            return CommonResult.error("不存在该产品");
        }
        return CommonResult.data(detail);
    }

    /**
     * 产品详情
     */
    @ApiOperationSupport(order = 2)
    @ApiOperation("根据podId获取profile文件")
    @CommonLog("根据podId获取profile文件")
    @GetMapping("/downloadProfile")
    public CommonResult<Object>  downloadProfile(@RequestParam @Valid String prodId) {
        String downloadProfile = productService.downloadProfile(prodId);
        String result = "";
        Object parse = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            log.info("获取路径{}",downloadProfile );
            URL url = new URL(downloadProfile);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                String line;
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                connection.disconnect();
                result = sb.toString();
                parse = JSON.parse(result);
            }
        } catch (IOException e) {
            log.error(">>> 获取profile文件异常：", e);
            return CommonResult.error("获取profile文件异常");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error(">>> 获取profile文件异常：", e);
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    log.error(">>> 获取profile文件异常：", e);
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.error(">>> 获取profile文件异常：", e);
                }
            }
        }
        return CommonResult.data(parse);
    }

}
